import 'babel-polyfill';
import AppDispatcher from '../dispatcher/dispatcher';
import Events from 'events';
import {department,doctors,comments,charts,skillTags,serverTags,rating,hospitals,title} from './data';

const EventEmitter = Events.EventEmitter;
const CHANGE_EVENT = 'change';

let _department = department;                   //科室（专科）
let _doctors = doctors;                         //医生
let _title = title;                             //职称
let _comments = comments;                       //评论
let _rating = rating;                //服务质量等级
let _chats = charts;                           //服务质量等级
let _hospitals = hospitals;                     //医院
let _mainNav = {show:true,active:1};            //主导航状态
let _date = ()=>{
    let d = new Date();
    return (d.getFullYear()+"-"+d.getMonth()+"-"+d.getDate())
}
const DoctorStore = Object.assign({},EventEmitter.prototype,{
    getDep(id){
        if(id){
            return _department[id]
        }else{
            return _department
        }
    },
    getTitle(id){
        if(id){
            return _title[id]
        }else{
            return _title
        }
    },
    getDoctor(id){
        return _doctors[id]
    },
    getComments(ids){
        let _out = [],
            i = 0;
        if(ids){
            for(let com in _comments){
                if(ids[i++].indexOf(com) !== (-1)){
                    _out.push(_comments[com])
                }
            }
        }
        if(_out.length>0){
            return _out
        }else{
            return '暂无评论'
        }
    },
    getDoctorsByDep(id){
        if(id in _department && _department[id].state != false){
            let doctors = [];
            let name = _department[id].name;
            for(let doc in _doctors){
                if(_doctors[doc].depart == id){
                    doctors.push(_doctors[doc])
                }
            }
            if(doctors.length > 0){
                return {
                    data:doctors,
                    len:doctors.length,
                    dep:name
                }

            }else{
                return `${name}没有合适的医生`
            }
        }else{
            return `${name}不存在`
        }
    },
    getRating(id){
       return _rating[id]
    },
    getChat(id){
        return _chats[id]
    },
    ctrlNav(obj){
        if(obj !== undefined){
            _mainNav.show = obj.show;
            _mainNav.active = obj.active;
        }
        return _mainNav
    },
    emitChange(){
        this.emit(CHANGE_EVENT)
    },
    addChangeListener(callback){
        this.on(CHANGE_EVENT,callback)
    },
    removeChangeListener(callback){
        this.removeListener(CHANGE_EVENT,callback)
    }
});
AppDispatcher.register((action) => {
    let obj;
    let id = '';
    switch (action.actionType){
        case 'GET_DEP':
            DoctorStore.getDep(id);
            DoctorStore.emitChange();
            break;
        case 'GET_TITLE':
            DoctorStore.getTitle(id);
            DoctorStore.emitChange();
            break;
        case 'GET_DOCTOR':
            id = action.id;
            DoctorStore.getDoctor(id);
            DoctorStore.emitChange();
            break;
        case 'GET_DOCTORS_BY_DEP':
            id = action.id;
            DoctorStore.getDoctorsByDep(id);
            DoctorStore.emitChange();
            break;
        case 'GET_COMMENTS':
            id = action.id;
            DoctorStore.getComments(id);
            DoctorStore.emitChange();
            break;
        case 'GET_RATING':
            id = action.id;
            DoctorStore.getRating(id);
            DoctorStore.emitChange();
            break;
        case 'GET_CHAT':
            id = action.id;
            DoctorStore.getChat(id);
            DoctorStore.emitChange();
            break;
        case 'CTRL_NAV':
            obj = action.data;
            DoctorStore.ctrlNav(obj);
            DoctorStore.emitChange();
            break;
        default:
            return null;
    }
});
export default DoctorStore